#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <cstdlib>
#include <map>
#include <bitset>
#include <unordered_map>
#include <ctime>
#include <vector>
#include <queue>
#include <list>

#define R register
#define L long
#define LL long long
#define I inline
#define U unsigned
using namespace std;

I LL read()
{
    R LL   x;
    R bool f;
    R char c;
    for(f=0;(c=getchar())<'0'||c>'9';f=c=='-');
    for(x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0');
    return f?-x:x;
}

#define MAXN 1000
struct node
{
    struct node*l,*r;
    int        value;
    char       code;
    bool operator()(const node *a,const node *b)
    {
        return a->value>b->value;
    }
};

struct DataT
{
    int  value;
    char code;
};

DataT Data[MAXN];

int getValue(char code,int n)
{
    for(int i=0;i<n;++i)
        if(Data[i].code==code)
            return Data[i].value;
    return 0;
}

node*newNode()
{
    node*tmp=(node*)malloc(sizeof(node));
    tmp->l=tmp->r=NULL;
    tmp->value=0;
    tmp->code =0;
    return tmp;
}

bool ERR_FLAG=false;

node*addTree(node*rt,char c,char s[],int Datan,int sn,int si)
{
    if(sn==si)
    {
        if(rt!=NULL)
        {
            ERR_FLAG=true;
            return rt;
        }
        else
        {
            node*tmp=newNode();
            tmp->value=getValue(c,Datan)*sn;
            return tmp;
        }
    }
    if(!rt)rt=newNode();
    if(s[si]=='0')
        rt->l=addTree(rt->l,c,s,Datan,sn,si+1);
    else
        rt->r=addTree(rt->r,c,s,Datan,sn,si+1);
    rt->value=((rt->l)?(rt->l->value):0)+((rt->r)?(rt->r->value):0);
    return rt;
}


int ddfs(node *rt,int n)
{
    if(!rt)
        return 0;
    if(rt->l||rt->r)
        rt->value=ddfs(rt->l,n+1)+ddfs(rt->r,n+1);
    else
    {
        rt->value*=n;
    }
    return rt->value;
}
int getAns(int n)
{
    priority_queue <node*,vector<node*>,node> q;
    for(int i=0;i<n;++i)
    {
        node * tmp=newNode();
        *tmp=(node){NULL,NULL,Data[i].value,Data[i].code};
        q.push(tmp);
    }
    
    while(q.size()>1)
    {
        node* t1=q.top();q.pop();
        node* t2=q.top();q.pop();
        node * tmp=newNode();
        tmp->l=t1;
        tmp->r=t2;
        tmp->value=t1->value+t2->value;
        q.push(tmp);
    }
    return  ddfs(q.top(),0);
}


int main()
{
    freopen("7-1.in","r",stdin);
//  freopen("7-1.out","w",stdout);
    int     n=read();
    for(int i=0;i<n;++i)
    {
        char s[10];
        scanf("%s%d",s,&Data[i].value);
        Data[i].code=s[0];
    }
    int ans=getAns(n);
    for(int t=read();t--;)
    {
        ERR_FLAG=false;
        node    *rt=NULL;
        for(int i  =0;i<n;++i)
        {
            char c[10],s[MAXN];
            scanf("%s%s",c,s);
            if(!ERR_FLAG)
                rt=addTree(rt,c[0],s,n,strlen(s),0);
        }
        printf("%s\n",(!ERR_FLAG&&(!rt||rt->value==ans))?"Yes":"No");
    }
    
    
    fclose(stdin);
    fclose(stdout);
    return 0;
}